package com.yequan.drawerlayoutdemo;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import com.nineoldandroids.view.ViewHelper;
import com.yequan.drawerlayoutdemo.databinding.ActivityMainBinding;
import com.yequan.drawerlayoutdemo.skin.base.BaseSkinActivity;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseSkinActivity implements CompanyInfoAdapter.OnCompanyItemClickListener {

    private static final String TAG = "MainActivity";
    private DrawerLayout mDrawerLayout;
    ActivityMainBinding binding;
    private CompanyInfoAdapter mAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        initView();
        initEvents();
        initData();
    }

    private void initData() {
        List<CompanyInfoEntry> companyInfoList = new ArrayList<>();
        companyInfoList.add(new CompanyInfoEntry("腾讯", "马化腾", true));
        companyInfoList.add(new CompanyInfoEntry("华为", "任正非", false));
        companyInfoList.add(new CompanyInfoEntry("阿里巴巴", "马云", true));
        companyInfoList.add(new CompanyInfoEntry("百度", "李彦宏", true));
        companyInfoList.add(new CompanyInfoEntry("京东", "刘强东", false));
        companyInfoList.add(new CompanyInfoEntry("网易", "丁磊", true));
        companyInfoList.add(new CompanyInfoEntry("apple", "库克", true));
        companyInfoList.add(new CompanyInfoEntry("Alphabet", "佩奇", true));
        companyInfoList.add(new CompanyInfoEntry("腾讯", "马化腾", true));
        companyInfoList.add(new CompanyInfoEntry("华为", "任正非", false));
        companyInfoList.add(new CompanyInfoEntry("阿里巴巴", "马云", true));
        companyInfoList.add(new CompanyInfoEntry("百度", "李彦宏", true));
        companyInfoList.add(new CompanyInfoEntry("京东", "刘强东", false));
        companyInfoList.add(new CompanyInfoEntry("网易", "丁磊", true));
        companyInfoList.add(new CompanyInfoEntry("apple", "库克", true));
        companyInfoList.add(new CompanyInfoEntry("Alphabet", "佩奇", true));
        companyInfoList.add(new CompanyInfoEntry("腾讯", "马化腾", true));
        companyInfoList.add(new CompanyInfoEntry("华为", "任正非", false));
        companyInfoList.add(new CompanyInfoEntry("阿里巴巴", "马云", true));
        companyInfoList.add(new CompanyInfoEntry("百度", "李彦宏", true));
        companyInfoList.add(new CompanyInfoEntry("京东", "刘强东", false));
        companyInfoList.add(new CompanyInfoEntry("网易", "丁磊", true));
        companyInfoList.add(new CompanyInfoEntry("apple", "库克", true));
        companyInfoList.add(new CompanyInfoEntry("Alphabet", "佩奇", true));
        mAdapter.addAll(companyInfoList);
    }

    private void initView() {
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        FragmentManager fm = getSupportFragmentManager();
        Fragment fragment = fm.findFragmentById(R.id.id_left_menu_container);
        if (fragment == null) {
            fm.beginTransaction().add(R.id.id_left_menu_container, new MenuItemFragment()).commit();
        }
    }

    private void initEvents() {
        mAdapter = new CompanyInfoAdapter(this);
        binding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        binding.recyclerView.setAdapter(mAdapter);
        mAdapter.setOnCompanyItemListener(this);

        mDrawerLayout.addDrawerListener(new DrawerLayout.DrawerListener() {
            @Override
            public void onDrawerSlide(View drawerView, float slideOffset) {
                View mContent = mDrawerLayout.getChildAt(0);
                View mMenu = drawerView;
                float scale = 1 - slideOffset;
                float rightScale = 0.8f + scale * 0.2f;
                if (drawerView.getTag().equals("LEFT")) {
                    float leftScale = 1 - 0.3f * scale;
                    ViewHelper.setScaleX(mMenu, leftScale);
                    ViewHelper.setScaleY(mMenu, leftScale);
                    ViewHelper.setAlpha(mMenu, 0.6f + 0.4f * (1 - scale));
                    ViewHelper.setTranslationX(mContent, mMenu.getMeasuredWidth() * (1 - scale));
                    ViewHelper.setPivotX(mContent, 0);
                    ViewHelper.setPivotY(mContent, mContent.getMeasuredHeight() / 2);
                    mContent.invalidate();
                    ViewHelper.setScaleX(mContent, rightScale);
                    ViewHelper.setScaleY(mContent, rightScale);
                }
            }

            @Override
            public void onDrawerOpened(View drawerView) {

            }

            @Override
            public void onDrawerClosed(View drawerView) {

            }

            @Override
            public void onDrawerStateChanged(int newState) {

            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_items, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int itemId = item.getItemId();
        switch (itemId) {
            case R.id.id_action_test_factory:
                Intent intent = new Intent(this, TestFactoryActivity.class);
                startActivity(intent);
                break;
        }
        return super.onOptionsItemSelected(item);
    }


    @Override
    public void onCompanyItemClick(CompanyInfoEntry companyInfoEntry) {
        if (LogConfig.ON) {
            Toast.makeText(this,
                    companyInfoEntry.getCompany() +
                            (companyInfoEntry.isMarket() ? "上市公司" : "非上市公司"),
                    Toast.LENGTH_SHORT).show();
        }
    }

}
